/* Copyright 2007 Ryan Phillips.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdarg.h>
#include <time.h>
#include "xblf.h"

#define XBLF_DELAY_TIME_SECS (60*10)  /**< 10 minutes */

typedef struct xblf_contacts_t {
    xblf_friend_t *f;
    int old_online;
    struct xblf_contacts_t *next;
} xblf_contacts_t;

xblf_contacts_t* xblf_contacts_new()
{
    return NULL;
}

void xblf_contacts_add_friend(xblf_contacts_t **c, const char *gamertag)
{
    xblf_contacts_t *contacts = (xblf_contacts_t*) calloc(1, sizeof(*contacts));

    contacts->f = xblf_friend_new_by_tag(gamertag);
    
    /* Add to Link List */
    contacts->next = *c;
    *c = contacts;
}

void xblf_contacts_presence_loop(xblf_contacts_t *c)
{
    xblf_contacts_t *contact = c;

    for (;;) {
        while (contact) {
            if (xblf_friend_query(contact->f) == XBLF_ERROR_SUCCESS) {
                int online_status = xblf_friend_presence_get_online(contact->f);
                char time_buf[200];
                time_t rawtime;
                struct tm *timeinfo;

                time(&rawtime);
                timeinfo = localtime(&rawtime);
                strftime(time_buf, sizeof(time_buf), "%a %b %d %H:%M :", timeinfo);

                if (online_status != contact->old_online) {
                    contact->old_online = online_status;
                    if (online_status) {
                        char *current_game = xblf_friend_presence_get_title(contact->f);
                        printf("%s %s is online playing %s\n", time_buf, contact->f->gamertag, current_game);
                        free(current_game);
                    }
                    else {
                        printf("%s %s went offline\n", time_buf, contact->f->gamertag);
                    }
                }
            }
            contact = contact->next;
        }

        /* Start over again */
        contact = c;

        sleep(XBLF_DELAY_TIME_SECS);
    }
}

void xblf_printf(int level, const char *fmt, ...)
{
    va_list args;
    int i;

    for (i=0; i<level; i++) {
        fprintf(stderr, "  ");
    }

    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
}

void banner()
{
    xblf_printf(0, "xblf presence\n");
    xblf_printf(0, "\n");
}

int main(int argc, const char *argv[])
{
    int i;
    xblf_contacts_t *contact_list;

    banner();

    xblf_init();

    contact_list = xblf_contacts_new();

    for (i=1; i<argc; i++) {
        xblf_printf(0, "Added friend : %s\n", argv[i]);
        xblf_contacts_add_friend(&contact_list, argv[i]);
    }

    if (!contact_list) {
        xblf_printf(0, "No friends added... exiting\n");        
    }
    else {
        xblf_contacts_presence_loop(contact_list);
    }

    /* TODO memory cleanup */

    xblf_finish();
    return 0;
}

